//
//  PrivacSetViewController.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/7.
//

import UIKit

class PrivacSetViewController: CommonViewController {
    private let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)
    private var dataList:Array<Array<SetItemModel>> = []
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.title = "隐私"
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints({
            $0.edges.equalToSuperview()
        })
        tableView.separatorColor = Separator_COLOR
        tableView.backgroundColor = BACKGROUND_COLOR

        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(SetItemArrowCell.self, forCellReuseIdentifier: "SettingArrowCell")
        tableView.register(SetItemSwitchCell.self, forCellReuseIdentifier: "SettingSwitchCell")
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.rowHeight = 44.0
        if DataManager.login{
            dataList.append([SetItemModel.init(type: .Arrow, title: "黑名单")])
        }
        dataList.append([SetItemModel.init(type: .Arrow, title: "广告设置")])
        dataList.append([SetItemModel.init(type: .Arrow, title: "个人信息收集设置",subTitle: "管理个人信息收集权限和系统数据访问权限"),SetItemModel.init(type: .Arrow, title: "清除历史阅读兴趣",subTitle: "清楚后,将无法根据历史兴趣记录推荐个性化资讯")])
        dataList.append([SetItemModel.init(type: .Switch, title: "个性化推荐", subTitle: "关闭后将无法看到个性化推荐内容"),SetItemModel.init(type: .Switch, title: "基于所在位置个性化推荐", subTitle: "允许我们使用你的位置信息向你推荐相关内容")])
        if DataManager.login{
            dataList.append([SetItemModel.init(type: .Switch, title: "保存草稿至云端", subTitle: "关闭后草稿将无法共享到其他设备")])
            dataList.append([SetItemModel.init(type: .Switch, title: "提前上传作品,减少等待时间")])
        }
        dataList.append([SetItemModel.init(type: .Arrow, title: "隐私条款"),SetItemModel.init(type: .Arrow, title: "应用权限说明"),SetItemModel.init(type: .Arrow, title: "第三方SDK目录")])

    }

}
extension PrivacSetViewController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        guard section != 1 else {
            return 40.0
        }
        return 10.0
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let item = dataList[indexPath.section][indexPath.row]
        if(item.subTitle == ""){
            return 44.0
        }else{
            return 66.0
        }
    }
   
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        SystemLog("didSelectRowAt")
        tableView.deselectRow(at: indexPath, animated: false)
    }
    
}
extension PrivacSetViewController:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataList[section].count
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataList.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let item = dataList[indexPath.section][indexPath.row]
        if item.type == .Arrow {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SettingArrowCell", for: indexPath) as? SetItemArrowCell
            cell?.binding(item)
            return cell!
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "SettingSwitchCell", for: indexPath) as? SetItemSwitchCell
            cell?.binding(item,indexPath:indexPath)
            return cell!
        }
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if(section == 1){
            let view = UIView()
            let titleL = UILabel()
            titleL.textColor = .lightGray
            titleL.text = "个人信息和隐私"
            titleL.font = Font_Size_12
            view.addSubview(titleL)
            titleL.snp.makeConstraints({
                $0.centerY.equalToSuperview()
                $0.left.equalToSuperview().inset(15)
            })
            let link = UIButton(type: .system)
            link.setTitle("信息管理指引", for: .normal)
            view.addSubview(link)
            link.titleLabel?.font = Font_Size_12
            link.snp.makeConstraints({
                $0.centerY.equalToSuperview()
                $0.right.equalToSuperview().inset(15)
            })
            link.addTarget(self, action: #selector(linkclick), for: .touchUpInside)
            return view
        }
        return nil
    }
    @objc func linkclick() {
        SystemLog("linkclick")
    }
}


